function validation() {

	// clear error messages // use for loop to clear
	U.$("err1").innerHTML = "";
	U.$("err2").innerHTML = "";
	U.$("err3").innerHTML = "";
	U.$("err4").innerHTML = "";
	U.$("err5").innerHTML = "";
	U.$("err6").innerHTML = "";
	U.$("err7").innerHTML = "";
	U.$("err8").innerHTML = "";
	U.$("err9").innerHTML = "";
	U.$("err10").innerHTML = "";
	
	var loanAmount = U.$('loanAmount').value;
	var years = U.$('years').value;
	var interestRate = U.$('interestRate').value;
	var firstName = U.$('firstName').value;
	var lastName = U.$('lastName').value;
	var email = U.$('email').value;
	var city  = U.$('city').value;
	var state = U.$('state').value;
	var zip = U.$('zip').value;

	var valid = true; 

	if (loanAmount == "" || isNaN(loanAmount)) {
	U.$("err1").innerHTML = "Loan Amount Must Be Numeric";
	valid = false; 
}
	if (years == "" || isNaN(years)) {
	U.$("err2").innerHTML = "Years Must Be Numeric!";
	valid = false; 
}
	if (interestRate == "" || isNaN(interestRate)){
	U.$("err3").innerHTML = "Interest Rate Must Be Numeric";
	valid = false; 
}
	if (firstName == "" || firstName.length < 2) {
	U.$("err4").innerHTML = "First Name Must Contain At Least One Character";
	valid = false; 
}
	if (lastName == "" || lastName.length < 2) {
	U.$("err5").innerHTML = "Last Name Must Contain At Least One Character";
	valid = false; 
}
	if (email == "" || email.length < 7 || email.indexOf('@') == -1 || email.indexOf('.') ==  -1 ) {
	U.$("err6").innerHTML = "Email Address Must Contain At Least 7 Characters and an '@' symbol";
	valid = false; 
}
	if (city == "") {
	U.$("err7").innerHTML = "City Must Not Be Blank" ;
	valid = false; 
}
if (zip == "" || zip.length !== 5 || isNaN(zip)) {
U.$("err9").innerHTML = "Zip Code Must be Numeric and be at least 5 characters" ;
valid = false; 
}
var genders = U.$('gender');
var checked = false; 
for(var i=0; i < gender.length; i++) {
if (genders[i].checked){
checked = true;
break;}}
if (checked === false){
U.$("err10").innerHTML = "Please Select a Gender" ;
valid = false;
}
var state = U.$('state').value;
if (state !== "Georgia" || state !== "Florida" ) {
U.$("err8").innerHTML = "Please Select a State" ;
valid = false; 
}
if (valid) {
calcPayment(loanAmount,interestRate,years,firstName,lastName);
}
function calcPayment(loanAmount,interestRate,years,firstName,lastName)
{
var monthlyPayment = (loanAmount * interestRate / 1200) / (1 - (1 / Math.pow(1 + interestRate / 1200, years * 12))); 
monthlyPayment = monthlyPayment.toFixed(2);
var output = U.$('output');
output.innerHTML = "Thank you, " + firstName + " " + lastName + ". Your monthly loan payment is $" + monthlyPayment +".";
}
 function login(){
 var users = ['Fred','Daphne','Velma','Shaggy'];
 var pws = ['1234','1234','1234','1234'];
 var userName = U.$('userName').value;
 var password = U.$('password').value;
 
 var found = false;
 for (var i = 0; i <users.length ; i++){
	if (userName === users[i] && password === pws[i]) {
		found = true;
		break;
		}
	}
	}
if (!found) {
U.$('loginErr').innerHTML = "Invalid username and password!";
} else { 
	window.location = "loanapplication.html";
	}
	}
function limitText() {
	   // to get the comments element
   var comments = U.$('comments');
   // to count the number of characters
    var count = comments.value.length;
    // to display the count
    U.$('count').innerHTML = count;    
    // to cut the overage
    if (count > 150) {
          comments.value = comments.value.slice(0,150);
    }
}	
function init() {
var calcButton = U.$('calcButton');
if (calcButton != null){
calcButton.onclick = validation;
}
var loginButton = U.$('loginButton');
if (loginButton != null) {
	loginButton.onclick = login;
	}
	var applyDate = U.$('applyDate');
	if (applyDate != null) {
		var today = new Date();
		applyDate.value = today.toUTCString(); } 
		
U.addEvent(U.$("comments"),"keyup", limitText);
	}
window.onload = init; 